﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace FrogEngine
{
	public class GameStateManagementService : GameEntityServiceBase<IGameScreen>, IGameStateManagementService
	{
		public GameStateManagementService(IEngine engine)
			: base(engine)
		{
			serviceOrder = 1;
			screenManager = new ScreenManager(engine);
		}

		public GameStateManagementService(IEngine engine, string fileName)
			: base(engine)
		{
			serviceOrder = 1;
			screenManager = new ScreenManager(engine, fileName);
			subscribedEntities = new HashSet<IGameEntity>(screenManager.Screens);
		}

		private ScreenManager screenManager;
		public ScreenManager ScreenManager
		{
			get { return screenManager; }
		}

		public override bool Subscribe(IGameEntity entity)
		{
			if (base.Subscribe(entity))
			{
				screenManager.AddScreen(entity as IGameScreen);
				return true;
			}
			else
				throw new EntityCompatibilityException(entity.GetType(), this.GetType());
		}

		public override bool Unsubscribe(IGameEntity entity)
		{
			if (base.Unsubscribe(entity))
			{
				screenManager.RemoveScreen(entity as IGameScreen);
				return true;
			}
			else 
				return false;
		}

		public void Update(GameTime gameTime)
		{
			screenManager.Update(gameTime);
		}

		public void Render(GameTime gameTime)
		{
			screenManager.Render(gameTime);
		}
	}
}
